package com.webserver.controller;

import com.webserver.annotation.Controller;
import com.webserver.annotation.RequestMapping;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import qrcode.QRCodeUtil;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

@Controller
public class ToolsController {

    @RequestMapping("/myweb/createQR")
    public void creatQR(HttpServletRequest request, HttpServletResponse response){
        String line = request.getParameter("content");
        response.setContentType("image/jpeg");

        try {
            QRCodeUtil.encode(line,"./Ikun.jpg",
                    response.getOutputStream(),//将生成的图片写出到数组中,目的是发送到正文中
                    true);
            System.out.println("二维码生成了");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/myweb/random.jpg")
    public void createRandomImage(HttpServletRequest request,HttpServletResponse response){
        response.setContentType("image/jpeg");

        //1.生成白纸
        BufferedImage image = new BufferedImage(70,30,
                BufferedImage.TYPE_INT_RGB
        );

        //2.生成画笔
        Graphics g = image.getGraphics();

        //3.随机生成背景色
        Random random = new Random();
        Color color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));


        g.setColor(color);//设置画笔颜色
        g.fillRect(0,0,70,30);//设置要涂的范围

        //4.向画布绘制文字
        String line = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        g.setFont(new Font(null,Font.BOLD,20));

        for (int i = 0; i < 4; i++) {
            color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
            g.setColor(color);
            int x = i*15 + 10;
            int y = 18 + random.nextInt(11)-5;
            String str = line.charAt(random.nextInt(line.length()))+"";
            g.drawString(str,x,y);
        }

        //5.画5条干扰线
        for (int i = 0; i < 5; i++) {
            color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
            g.setColor(color);
            g.drawLine(random.nextInt(71),random.nextInt(31),random.nextInt(71),random.nextInt(31));
        }




        try {
            ImageIO.write(image,"jpg",response.getOutputStream());
            System.out.println("图片已生成");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

//    public static void main(String[] args) {
//        //1.生成白纸
//        BufferedImage image = new BufferedImage(70,30,
//                BufferedImage.TYPE_INT_RGB
//        );
//
//        //2.生成画笔
//        Graphics g = image.getGraphics();
//
//        //3.随机生成背景色
//        Random random = new Random();
//        Color color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
//
//
//        g.setColor(color);//设置画笔颜色
//        g.fillRect(0,0,70,30);//设置要涂的范围
//
//        //4.向画布绘制文字
//        String line = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
//        g.setFont(new Font(null,Font.BOLD,20));
//
//        for (int i = 0; i < 4; i++) {
//            color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
//            g.setColor(color);
//            int x = i*15 + 10;
//            int y = 18 + random.nextInt(11)-5;
//            String str = line.charAt(random.nextInt(line.length()))+"";
//            g.drawString(str,x,y);
//        }
//
//        //5.画5条干扰线
//        for (int i = 0; i < 5; i++) {
//            color = new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256));
//            g.setColor(color);
//            g.drawLine(random.nextInt(71),random.nextInt(31),random.nextInt(71),random.nextInt(31));
//        }
//
//
//
//
//        try {
//            ImageIO.write(image,"jpg",new File("./random.jpg"));
//            System.out.println("图片已生成");
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//
//
//
//    }

//    public static void main(String[] args) {
//        String line = "扫你妹！";
//        try {
////            QRCodeUtil.encode(line,"./qr.jpg");
//
//            QRCodeUtil.encode(line,"./Ikun.jpg","./qr.jpg",true);
//            System.out.println("二维码已生成");
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
}
